<template>
  <div id="app">
    <el-card shadow="hover">
      {{condition}}
      <!--条件查询-->
      <el-form :inline="true" size="medium" :model="condition" class="demo-form-inline">
        <el-form-item >
          <el-input v-model="condition.name" placeholder="学生姓名"></el-input>
        </el-form-item>
        <el-form-item >
          <el-input v-model="condition.addr" placeholder="学生住址"></el-input>
        </el-form-item>
        <el-form-item >
          <el-select v-model="condition.cid" placeholder="所在班级">
            <el-option label="所有班级" value="0"></el-option>
            <el-option :label="c.cname" :value="c.cid" v-for="c in classesList" :key="c.cid"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" plain @click="conditionSearch" icon="el-icon-user">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="success" plain @click="openAddStudent" icon="el-icon-plus">添加学生</el-button>
        </el-form-item>
      </el-form>
      <el-table
          :data="studentList"
          style="width: 100%">
        <el-table-column
            label="学号"
            prop="id"
            align="center"
            width="180">
        </el-table-column>
        <el-table-column
            label="姓名"
            align="center"
            prop="name">
        </el-table-column>
        <el-table-column
            label="性别"
            align="center"
            prop="sex">
        </el-table-column>
        <el-table-column
            label="年龄"
            align="center"
            prop="age">
        </el-table-column>
        <el-table-column
            label="住址"
            align="center"
            prop="addr">
        </el-table-column>
        <el-table-column
            label="所在班级"
            align="center"
            prop="cname">
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button
                size="small"
                @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button
                size="small"
                type="danger"
                @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!--分页导航-->
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </el-card>
    <!--对话框-->
    <el-dialog :title="title" :close-on-click-modal="false" :visible.sync="dialogFormVisible">
      {{student}}
      <el-form :model="student">
        <el-form-item label="学生姓名" :label-width="formLabelWidth">
          <el-input v-model="student.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="学生性别" :label-width="formLabelWidth">
          <el-radio v-model="student.sex" label="男">男</el-radio>
          <el-radio v-model="student.sex" label="女">女</el-radio>
        </el-form-item>
        <el-form-item label="学生年龄" :label-width="formLabelWidth">
          <el-input v-model="student.age" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="学生住址" :label-width="formLabelWidth">
          <el-input v-model="student.addr" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="所在班级" :label-width="formLabelWidth" style="width: 1200px">
          <el-select v-model="student.cid" placeholder="请选择所在班级" >
            <el-option label="所有班级" value="0"></el-option>
            <el-option :label="c.cname" :value="c.cid" v-for="c in classesList" :key="c.cid"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addStudent">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import student from "@/api/student";
import classes from "@/api/classes";
export default {
  data() {
    return {
      condition: {},
      student:{},
      classesList: [],
      studentList: [],      // 当前页的所有学生信息集合
      page:1,               // 当前页
      pageSize:5,           // 每页大小
      total:0,            // 总记录数
      title: '添加学生',
      dialogFormVisible: false,
      formLabelWidth: '120px'
    }
  },
  created() {
    // // 1. 进行分页查询
    // this.findByPage(this.page,this.pageSize)
    this.conditionSearch()
    this.findClasses()

  },
  methods: {
    // // 1. 查询所有学生
    // findStudents(){
    //   this.$axios.get('/student/list.do')
    //       .then(res => {
    //         console.log(res.data)
    //         this.studentList = res.data
    //       })
    // },
    // // 2. 进行分页查询
    // findByPage(page,pageSize){
    //   this.$axios.get(`/student/findByPage.do?page=${page}&pageSize=${pageSize}`).then(res => {
    //     console.log(res)
    //     this.studentList = res.data.rows
    //     this.total = res.data.total
    //   })
    // },
    async findClasses() {
      const res = await classes.findClasses()
      this.classesList = res.data
    },

    async conditionSearch() {
      const res = await student.search(this.page, this.pageSize, this.condition)
      this.studentList = res.data.rows
      this.total = res.data.total
    },
    openAddStudent(){
      this.dialogFormVisible = true
      this.title = '添加学生'
    },
    async addStudent() {

      const res = await student.save(this.student)
      if (res.data.code === 200) {
        this.$message.success(res.data.message)
        this.dialogFormVisible = false
        this.student = {}
        await this.conditionSearch()
      }else {
        this.$message.error(res.data.message)
      }
    },
    handleEdit(index, row) {
    },
    handleDelete(index, row) {
    },
    // 每页大小改变时事件
    handleSizeChange(val) {
      this.pageSize = val;
      this.conditionSearch()
    },
    // 当前页改变时事件
    handleCurrentChange(val) {
      this.page = val;
      this.conditionSearch()
    }
  }
}
</script>
<style>

</style>
